package waal.pc.frontend.config;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

import waal.pc.backend.hub.Hub;
import waal.pc.backend.module.Module;
import waal.pc.frontend.Surface;
import waal.pc.frontend.WaalActions;
import waal.pc.frontend.tiles.Tile;
import waal.pc.frontend.tiles.TileHandler;

public class WaalConfig {

	private Surface surface;
	private JPanel mainPane;
	private HubArea hubArea;
	private ModuleArea moduleArea;
	private ChannelArea channelArea;
	private GridBagLayout gbl;
	private TileHandler tileHandler;	
	
	public WaalConfig(Surface surface) {
		this.surface = surface;
		this.tileHandler = new TileHandler();
		this.hubArea = new HubArea(this);
		this.moduleArea = new ModuleArea(this, this.tileHandler);
		this.channelArea = new ChannelArea(this, this.tileHandler);
		this.surface.addHubEventListener(hubArea);
		this.gbl = new GridBagLayout();
		this.mainPane = new JPanel(this.gbl);
		//JPanel hubPane = new JPanel();
		this.addComponent(this.hubArea, 			1, 0, 1, 1, 1, 1);
		this.addComponent(this.moduleArea, 			2, 0, 1, 1, 1, 1);
		this.addComponent(this.channelArea,		 	3, 0, 1, 1, 1, 1);
	}
	
	public WaalActions getWaalAction() {
		return this.surface.getWaalActions();
	}
	
	public JPanel getPanel() {
		return this.mainPane;
	}
	
	public void makeModulesForHub(Hub hub) {
		this.moduleArea.makeModulesForHub(hub);
	}
	
	public void setActiveHub(Hub hub) {
		this.moduleArea.setActiveHub(hub);
	}
	
	public void makeChannelsForModule(Module module) {
		this.channelArea.makeChannelsForModule(module);	}
	
	public void setActiveModule(Module module) {
		this.channelArea.setActiveModule(module);
	}
	
	public void addTileToSurface(Tile tile, int position) {
		this.surface.addTile(tile, position);
		this.updateFreeTileNumber(position +1);
	}
	
	private void addComponent(Component c, int x, int y, int width, int height, double weightx, double weighty ) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = x; gbc.gridy = y;
		gbc.gridwidth = width; gbc.gridheight = height;
		gbc.weightx = weightx; gbc.weighty = weighty;
		this.gbl.setConstraints( c, gbc );
		//c.setPreferredSize(new Dimension(Surface.tile_x, Surface.tile_y));
		this.mainPane.add( c );
		//this.surface.pack();
	}
	
	public void updateFreeTileNumber(int i) {
		if(i <= 6) {
			this.channelArea.updateFreeTileNumber(i);
			this.moduleArea.updateFreeTileNumber(i);
		}
	}
	
	
	
}
